package com.example.pkmedical.Activity.warning;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SerialProtUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class LeaveBedActivity extends BaseActivity {

    @BindView(R.id.main_house)
    ImageView mainHouse;
    @BindView(R.id.main_house_text)
    TextView mainHouseText;
    @BindView(R.id.warning_leave_position_img)
    ImageView warningLeavePositionImg;
    @BindView(R.id.warning_leave_position_text)
    TextView warningLeavePositionText;
    @BindView(R.id.warning_leave_position_layout)
    RelativeLayout warningLeavePositionLayout;
    @BindView(R.id.warning_leave_ing_img)
    ImageView warningLeaveIngImg;
    @BindView(R.id.warning_leave_ing_text)
    TextView warningLeaveIngText;
    @BindView(R.id.warning_leave_ing_layout)
    RelativeLayout warningLeaveIngLayout;
    @BindView(R.id.warning_leave_ed_img)
    ImageView warningLeaveEdImg;
    @BindView(R.id.warning_leave_ed_text)
    TextView warningLeaveEdText;
    @BindView(R.id.warning_leave_ed_layout)
    RelativeLayout warningLeaveEdLayout;
    @BindView(R.id.warning_leave_history)
    Button warningLeaveHistory;
    @BindView(R.id.warning_leave_goback)
    Button warningLeaveGoback;
    @BindView(R.id.warning_leave_close_text)
    TextView warningLeaveCloseText;
    @BindView(R.id.warning_leave_close_layout)
    RelativeLayout warningLeaveCloseLayout;

    private int mWarningType = 0;// 离床报警选中类型：1：位置，2正在，3：已经，0，关闭

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leavebed);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        allOff();
        switch (CommandUtil.LEAVEBED_TYPE) {
            case 0:
                closeOn();
                break;
            case 1:
                positionOn();
                break;
            case 2:
                ingOn();
                break;
            case 3:
                edOn();
                break;
        }
    }

    @Override
    public void initView() {
        mainHouse.setImageResource(R.drawable.img_title_warning);
        mainHouseText.setText(R.string.title_warning);
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.warning_leave_close_layout, R.id.warning_leave_position_layout, R.id.warning_leave_ing_layout, R.id.warning_leave_ed_layout, R.id.warning_leave_history, R.id.warning_leave_goback})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.warning_leave_position_layout:
                allOff();
                positionOn();
                if (0 != CommandUtil.LEAVEBED_TYPE) {
                    startActivity(new Intent(MyApp.getAppContext(), LeaveBedStartingActivity.class));
                    LeaveBedActivity.this.finish();
                }
                break;
            case R.id.warning_leave_ing_layout:
                allOff();
                ingOn();
                if (0 != CommandUtil.LEAVEBED_TYPE) {
                    startActivity(new Intent(MyApp.getAppContext(), LeaveBedStartingActivity.class));
                    LeaveBedActivity.this.finish();
                }
                break;
            case R.id.warning_leave_ed_layout:
                allOff();
                edOn();
                if (0 != CommandUtil.LEAVEBED_TYPE) {
                    startActivity(new Intent(MyApp.getAppContext(), LeaveBedStartingActivity.class));
                    LeaveBedActivity.this.finish();
                }
                break;
            case R.id.warning_leave_close_layout:
                allOff();
                closeOn();
                SerialProtUtil.getInstance().sendBytes50(2, CommandUtil.GET_WEIGHT_LEAVE_STOP);
                break;
            case R.id.warning_leave_history:

                break;
        }

    }

    private void positionOn() {
        CommandUtil.LEAVEBED_TYPE = 1;
        warningLeavePositionLayout.setBackgroundResource(R.drawable.changgui_item_back_on);
        warningLeavePositionImg.setImageResource(R.drawable.img_leave_position_on);
        warningLeavePositionText.setTextColor(getResources().getColor(R.color.text_black));
    }

    private void positionOff() {

        warningLeavePositionLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        warningLeavePositionImg.setImageResource(R.drawable.img_leave_position_off);
        warningLeavePositionText.setTextColor(getResources().getColor(R.color.white));
    }

    private void ingOn() {
        CommandUtil.LEAVEBED_TYPE = 2;
        warningLeaveIngLayout.setBackgroundResource(R.drawable.changgui_item_back_on);
        warningLeaveIngImg.setImageResource(R.drawable.img_leave_ing_on);
        warningLeaveIngText.setTextColor(getResources().getColor(R.color.text_black));
    }

    private void ingOff() {

        warningLeaveIngLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        warningLeaveIngImg.setImageResource(R.drawable.img_leave_ing_off);
        warningLeaveIngText.setTextColor(getResources().getColor(R.color.white));
    }

    private void edOn() {
        CommandUtil.LEAVEBED_TYPE = 3;
        warningLeaveEdLayout.setBackgroundResource(R.drawable.changgui_item_back_on);
        warningLeaveEdImg.setImageResource(R.drawable.img_leave_ed_on);
        warningLeaveEdText.setTextColor(getResources().getColor(R.color.text_black));
    }

    private void edOff() {

        warningLeaveEdLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        warningLeaveEdImg.setImageResource(R.drawable.img_leave_ed_off);
        warningLeaveEdText.setTextColor(getResources().getColor(R.color.white));
    }

    private void closeOn() {
        CommandUtil.LEAVEBED_TYPE = 0;
        warningLeaveCloseLayout.setBackgroundResource(R.drawable.changgui_item_back_on);
        warningLeaveCloseText.setTextColor(getResources().getColor(R.color.text_black));
    }

    private void closeOff() {

        warningLeaveCloseLayout.setBackgroundResource(R.drawable.changgui_item_back_off);
        warningLeaveCloseText.setTextColor(getResources().getColor(R.color.white));
    }

    private void allOff() {
        positionOff();
        ingOff();
        edOff();
        closeOff();
    }

    @OnClick(R.id.warning_leave_goback)
    public void onViewClicked() {
        LeaveBedActivity.this.finish();
    }

}
